package com.etalk.crm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etalk.crm.utils.EncryptAndDecrypt;
import com.etalk.crm.utils.HttpUtils;
import okhttp3.Headers;
import okhttp3.Request;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Jordan
 * @date: 2019/1/9 18:12
 * @description: 容联·七陌 网络电话呼叫系统
 */
@RestController
@RequestMapping("/7moor")
public class CallingPhoneController {
    @Resource
    private HttpSession session;

    @PostMapping("/dialing")
    public Map<String,Object> dialingPhone(String phone,int type){
        Map<String,Object> resultMap=new HashMap<>(2);
        if (type<1 || type>3){
            resultMap.put("status", 0);
            resultMap.put("errMsg", "手机接听类型错误");
            return resultMap;
        }
        //账号
        String account = "N00000023135";
        //api密码
        String secret = "f90b0280-3e0f-11e8-b318-57a8debd96b0";
        //拨打电话方式:Local 绑定手机 sip 企话宝接听  gateway 专用设备
        String[] extendType=new String[]{"Local","sip","gateway"};
        //接口鉴权密钥
        String time=this.getDateTime();
        String sig = EncryptAndDecrypt.MD5(account + secret + time);
        //header授权码
        String auth = EncryptAndDecrypt.base64(account + ":" + time);

        String fromExtend=session.getAttribute("accountId").toString().trim();
        if(fromExtend.contains("@")) {
            fromExtend=fromExtend.substring(0, fromExtend.indexOf("@"));
        }

        String httpUrl="http://apis.7moor.com/v20160818/call/dialout/"+account+"?sig="+sig;
        Map<String,Object> dataMap=new HashMap<>(4);
        dataMap.put("FromExten",fromExtend);
        dataMap.put("Exten",phone);
        dataMap.put("ExtenType",extendType[type-1]);

        String result=HttpUtils.httpPost(httpUrl, JSON.toJSONString(dataMap),auth);
        if (!StringUtils.isEmpty(result)) {
            JSONObject resultJson = JSON.parseObject(result);
            if (!resultJson.getBooleanValue("Succeed")) {
                resultMap.put("status", 0);
                if (resultJson.getString("Message").equals("409 Agent extenType not available")) {
                    if (type == 1) {
                        resultMap.put("errMsg", "未绑定手机号");
                    } else {
                        resultMap.put("errMsg", "软电话(企话宝)未登陆");
                    }
                }else {
                    resultMap.put("errMsg", resultJson.getString("message"));
                }
            } else {
                resultMap.put("status", 1);
            }
        }
        return resultMap;
    }

    private String getDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }
}
